package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
@Api(tags = "管理端订单相关接口")
@Slf4j
@Component
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 订单搜索
     *
     * @param dto dto
     * @return R
     */
    @GetMapping("/conditionSearch")
    public Result<PageBean> conditionSearch(OrdersPageQueryDTO dto) {
        PageBean pageBean = orderService.selectOrders(dto);
        return Result.success(pageBean);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return R
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> OrderStatusQuantityStatistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.selectOrderStatusQuantityStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 完成订单
     *
     * @param id 订单id
     * @return R
     */
    @PutMapping("/complete/{id}")
    public Result<String> completeOrder(@PathVariable Long id) {
        orderService.completeOrder(id);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @param dto dto
     * @return R
     */
    @PutMapping("/rejection")
    public Result<String> rejectionOrder(@RequestBody OrdersRejectionDTO dto) {
        orderService.rejectionOrder(dto);
        return Result.success();
    }

    /**
     * 取消订单
     *
     * @param dto dto
     * @return R
     */
    @PutMapping("/cancel")
    public Result<String> cancelOrder(@RequestBody OrdersCancelDTO dto) {
        orderService.cancelOrderWithReason(dto);
        return Result.success();
    }

    /**
     * 接单
     *
     * @param dto dto
     * @return R
     */
    @PutMapping("/confirm")
    public Result<String> confirmOrder(@RequestBody OrdersConfirmDTO dto) {
        orderService.confirmOrder(dto);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id 订单id
     * @return R
     */
    @PutMapping("/delivery/{id}")
    public Result<String> deliveryOrder(@PathVariable Long id) {
        orderService.deliveryOrder(id);
        return Result.success();
    }

    /**
     * 查询订单详情
     *
     * @param id 订单id
     * @return R
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> selectOrderWithDetail(@PathVariable Long id) {
        OrderVO orderVO = orderService.selectOrderWithDetail(id);
        return Result.success(orderVO);
    }


}
